Public Class frmTabelle

    Private Function Controllo() As Boolean
        ' controlla che le espressioni dei numeri decimali siano corrette
        Dim R As New System.Text.RegularExpressions.Regex("^\d+([\,\.]\d{1,2})?$")

        If Not R.IsMatch(txtSpese.Text) Or Not R.IsMatch(txtLampada.Text) Then
            MsgBox("Valore numerico inserito errato. Consentite solo due cifre decimali", MsgBoxStyle.Critical, "Controllo numerico")
            Controllo = False
        Else
            Controllo = True
        End If

    End Function

    Private Sub frmTabelle_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ' salva i nuovi valori
        Dim cnDb As OleDb.OleDbConnection
        Dim cmDb As OleDb.OleDbCommand
        Dim strSql As New System.Text.StringBuilder

        If iva <> CSng(txtIva.Text) Or spese <> CSng(txtSpese.Text) Or costoLampada <> CSng(txtLampada.Text) Or numCopieContratto <> txtNumCopie.Text Then
            If Controllo() Then
                cnDb = New OleDb.OleDbConnection(strConnessioneClienti)
                cnDb.Open()
                strSql.Append("UPDATE DATI SET SPESE_ALLACCIAMENTO=")
                strSql.Append(VirgolaOrPunto(txtSpese.Text, ","))
                strSql.Append(", IVA=")
                strSql.Append(VirgolaOrPunto(txtIva.Text, ","))
                strSql.Append(", IMPORTO_LAMPADA=")
                strSql.Append(VirgolaOrPunto(txtLampada.Text, ","))
                strSql.Append(", NUM_COPIE=")
                strSql.Append(CSng(txtNumCopie.Text))

                cmDb = New OleDb.OleDbCommand(strSql.ToString, cnDb)
                cmDb.ExecuteNonQuery()

                iva = CSng(txtIva.Text)
                spese = CSng(txtSpese.Text)
                costoLampada = CSng(txtLampada.Text)
                numCopieContratto = txtNumCopie.Text

                cnDb.Close()
            Else
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub frmTabelle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Top = 10
        Me.Left = 10

        txtIva.Text = iva
        txtLampada.Text = costoLampada
        txtSpese.Text = spese
        txtNumCopie.Text = numCopieContratto
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.MdiParent = frmMain
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtIva.KeyDown, txtLampada.KeyDown, txtSpese.KeyDown, txtNumCopie.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub txt_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLampada.KeyPress, txtIva.KeyPress, txtSpese.KeyPress
        If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 44 Or Asc(e.KeyChar) = 8) Then
            e.KeyChar = ""
        End If
    End Sub

    Private Sub txtNumCopie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumCopie.KeyPress
        If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8) Then
            e.KeyChar = ""
        End If
    End Sub

    Private Sub cmdChiudi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChiudi.Click
        Me.Close()
    End Sub
End Class